package com.mj.app.health.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mj.app.common_util.picasso.ImageLoader;
import com.mj.app.health.MyApplication;
import com.mj.app.health.R;
import com.mj.app.health.activity.LoginActivity;
import com.mj.app.health.activity.ServiceActivity;
import com.mj.app.health.activity.TimeRecordActivity;
import com.mj.app.health.base.BaseFragment;
import com.mj.app.health.bean.UserBean;
import com.mj.app.health.defination.IntentKeys;
import com.mj.app.health.dialog.CommonNoticeDialog;
import com.mj.app.health.dialog.CommonNoticeDialogSingle;
import com.mj.app.health.dialog.ICommonDialog;
import com.mj.app.health.dialog.ICommonDialogSingle;
import com.mj.app.health.net.HttpGo;
import com.mj.app.health.net.HttpListener;
import com.mj.app.health.net.response.SignResponse;
import com.mj.app.health.net.response.UserRefreshResponse;
import com.mj.app.health.util.SharedPreferanceUtil;
import com.mj.app.health.util.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * ========================================
 * *
 * title: SelfFragment
 * description:
 * *
 * Created by David Hogan on 2018/3/2.
 * ========================================
 */

public class SelfFragment extends BaseFragment implements View.OnClickListener{

    @BindView(R.id.self_head)
    ImageView selfHead;
    @BindView(R.id.self_name)
    TextView selfName;
    @BindView(R.id.sm_zengji)
    LinearLayout smZengji;
    @BindView(R.id.sm_jianfei)
    LinearLayout smJianfei;
    @BindView(R.id.self_sign)
    TextView selfSign;
    @BindView(R.id.self_logout)
    TextView logout;
    @BindView(R.id.sm_jishi)
    LinearLayout smJishi;


    private Handler mHandler;

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_self;
    }

    @Override
    protected void init() {

        ImageLoader.loadCornerWithResId(R.mipmap.ic_launcher, selfHead, R.mipmap.ic_launcher);
        selfSign.setSelected(true);
        selfName.setText(MyApplication.getUser().getUserName());

        mHandler = new Handler() {
            @Override
            public void handleMessage(final Message msg) {

                if(msg.what == 1){ // 签到成功
                    selfSign.setSelected(true);
                    selfSign.setOnClickListener(null);
                    showNoticeDialog();
                }else if(msg.what == 2){ // 签到失败
                    ToastUtil.show(msg.obj.toString());
                }
                else if(msg.what == 11){

                    //未签到
                    selfSign.setSelected(false);
                    selfSign.setOnClickListener(SelfFragment.this);
                    selfName.setText(MyApplication.getUser().getUserName());
                }else if(msg.what == 12){

                    // 已经签到
                    selfSign.setSelected(true);
                    selfName.setText(MyApplication.getUser().getUserName());
                }
            }
        };

        new Thread(new Runnable() {
            @Override
            public void run() {
                final Message msg = new Message();

                if("1".equals(SharedPreferanceUtil.getSp("IfHasLogined",""))){
                    HttpGo.refreshUser(MyApplication.getUser().getUserId(), new HttpListener<UserRefreshResponse>() {
                        @Override
                        public void onSuccess(UserRefreshResponse userRefreshResponse) {
                            UserBean userBean = userRefreshResponse.getData();

                            if(userBean != null){

                                if(!userBean.isUserIsNeedSign()){
                                    // 已经签到
                                    msg.what = 12;
                                    mHandler.sendMessage(msg);
                                }else{
                                    msg.what = 11;
                                    mHandler.sendMessage(msg);
                                }

                            }
                        }

                        @Override
                        public void onError(String exception) {
                            ToastUtil.show(exception);
                            msg.what = 0;
                            mHandler.sendMessage(msg);
                        }
                    });
                    logout.setVisibility(View.VISIBLE);
                }else{

                    logout.setVisibility(View.GONE);
                }

            }
        }).start();

        setListener();
    }



    // 签到
    private void sign(){

        final Message msg = new Message();
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpGo.sign(MyApplication.getUser().getUserId(), new HttpListener<SignResponse>() {
                    @Override
                    public void onSuccess(SignResponse signResponse) {

                        if(signResponse.getCode() == 1){
                            msg.what =1;
                            mHandler.sendMessage(msg);
                        }else{
                            msg.what =2;
                            msg.obj =signResponse.getMsg();

                            mHandler.sendMessage(msg);
                        }

                    }

                    @Override
                    public void onError(String exception) {
                        ToastUtil.show(exception);
                    }
                });
            }
        }).start();

    }

    private void showNoticeDialog(){
        CommonNoticeDialogSingle noticeDialogSingle = new CommonNoticeDialogSingle(getActivity());
        noticeDialogSingle.setMsgTxt("签到成功");
        noticeDialogSingle.setDialogSingle(new ICommonDialogSingle() {
            @Override
            public void onSingleSurePressed() {

            }
        });
        noticeDialogSingle.show();
    }

    @Override
    public void onClick(View v) {
        Bundle b = new Bundle();
        switch (v.getId()){
            case R.id.self_sign: // 签到
                sign();
                break;
            case R.id.sm_jianfei: // 减肥
                b.putInt(IntentKeys.WEB_FROM, 4);
                redirectTo(ServiceActivity.class, false, b);
                setAnimationIn();
                break;
            case R.id.sm_zengji: // 增肌
                b.putInt(IntentKeys.WEB_FROM, 5);
                redirectTo(ServiceActivity.class, false, b);
                setAnimationIn();
                break;

            case R.id.self_logout: // 登出

                CommonNoticeDialog commonNoticeDialog = new CommonNoticeDialog(getActivity());
                commonNoticeDialog.setMsgTxt("确定要退出登录吗？");
                commonNoticeDialog.setICommonDialog(new ICommonDialog() {
                    @Override
                    public void onSurePressed() {

                        SharedPreferanceUtil.clear();
                        redirectTo(LoginActivity.class);
                        getActivity().finish();
                        setAnimationIn();
                    }

                    @Override
                    public void onCancelPressed() {

                    }
                });
                commonNoticeDialog.show();

                break;

            case R.id.sm_jishi: // 运动即时

                redirectTo(TimeRecordActivity.class);
                setAnimationIn();
                break;
        }
    }

    private void setListener() {
        logout.setOnClickListener(this);
        smZengji.setOnClickListener(this);
        smJianfei.setOnClickListener(this);
        smJishi.setOnClickListener(this);
    }
}
